package com.example.mybatis.flex.config;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

@Configuration
public class DenodoDatabaseIdProviderConfig {
    @Bean
    public DatabaseIdProvider databaseIdProvider() {
        return new DenodoDatabaseIdProvider();
    }

    class DenodoDatabaseIdProvider implements DatabaseIdProvider {

        @Override
        public String getDatabaseId(DataSource dataSource) throws SQLException {
            DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
            String databaseProductName = metaData.getDatabaseProductName();
            if ("Virtual DataPort".equals(databaseProductName)) {
                return "denodo";
            }
            return null;
        }

    }

}